package myjava;


import java.util.Random;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // write your code here
        Mapmaker gamestart = new Mapmaker();
        System.out.println("请输入难度：");
        Scanner sc= new Scanner(System.in);
        int hd=sc.nextInt();

        String nowmap[][] = gamestart.map(hd);
        gamestart.Usercontroll(nowmap);

       // String nowmap[][] = gamestart.map(hd);
        //gamestart.showmaps(nowmap);
        while (true) {

            if (gamestart.gameover=true){
                break;
            }
            if (gamestart.restart==true){
                gamestart.restart=false;
                continue;

            }
        }

    }
}



class Mapmaker{
    boolean gameover;
    boolean restart;

    public static String[][] map(int hardlevel) {

        String[][] newmap = new String[10][8];
        for (int i = 0; i < newmap.length; i++) { //用于数组 map所有的填充为空格
            for (int j = 0; j < newmap[0].length; j++) {
                newmap[i][j] = "  ";
            }
        }
            for (int i = 0; i < newmap.length; i++) { //构建左右2侧的H围墙
                newmap[i][0] = "H ";
                newmap[i][newmap[0].length - 1] = "H ";

            }
            for (int j = 0; j < newmap[0].length; j++) { //构建上下2侧的H围墙
                newmap[0][j] = "H ";
                newmap[newmap.length - 1][j] = "H ";
            }

        int count=0; //用于统计难度
            while (true){ //构建随机围墙

                Random radwall= new Random();
                int x=radwall.nextInt(newmap[0].length-2)+1; //定义随机墙x坐标范围
                int y=radwall.nextInt(newmap.length-3)+1;//定义随机墙y坐标范围
            //    System.out.println(x+" x y"+y);
                if ((x ==1  && y==1) || ( x==2 && y==2)|| ( x==7 && y==7)){ //忽略用户、箱子、胜利点坐标。
                    continue;

                }else {
                    newmap[x][y]="H ";
                    count++;

                }
                if (count==hardlevel){
                    break;
                }
            }


            return newmap;
        }


    public void showmaps(String[][] map){ //地图输出
        for(int i=0;i<map.length;i++){
            for(int j=0;j<map[0].length;j++){
                System.out.print(map[i][j]);
            }
            System.out.println("");

        }
    }


    public void Usercontroll(String[][] user){
        int x=1;
        int y=1;
        user[x][y]="& ";
        user[2][2]="o ";
        user[7][6]="* ";
        showmaps(user);
        while (true){ //定义如果 o 与* 相邻，则判断游戏胜利。
            if(user[7][5]=="o "||user[6][6]=="o "){
                System.out.println("you are winner!!!!");
                gameover=true;
                break;
            }
            if (restart==true){
                break;
            }
            Scanner sc =new Scanner(System.in);
            System.out.print("输入指令(ASDW)：");
            String userinput=sc.nextLine();
            switch (userinput){
                case "a":   //定义向左移动
                    if (user[x][y-1]=="H "){
                        showmaps(user);
                       break;
                    }else{
                        if ((user[x][y-1]=="o ") && (user[x][y-2]=="  ")){
                            user[x][y-2]=user[x][y-1];
                            user[x][y-1]=user[x][y];
                            user[x][y]="  ";
                            y=y-1;
                            showmaps(user);
                            break;
                            } else{
                            user[x][y-1]=user[x][y];
                            user[x][y]="  ";
                            y=y-1;
                            showmaps(user);
                            break;

                        }
                        }

                case "s": //定义向下移动

                    if (user[x+1][y]=="H "){
                        showmaps(user);
                        break;
                    }else{
                        if ((user[x+1][y]=="o ") && (user[x+2][y]=="  ")){
                            user[x+2][y]=user[x+1][y];
                            user[x+1][y]=user[x][y];
                            user[x][y]="  ";
                            x=x+1;
                            showmaps(user);
                            break;
                        } else{
                            System.out.println("s ok is called");
                            user[x+1][y]=user[x][y];
                            user[x][y]="  ";
                            x=x+1;
                            showmaps(user);
                            break;

                        }
                    }

                case "w": //定义向上移动
                    if (user[x-1][y]=="H "){
                        showmaps(user);
                        break;
                    }else{
                        if ((user[x-1][y]=="o ") && (user[x-2][y]=="  ")){
                            user[x-2][y]=user[x-1][y];
                            user[x-1][y]=user[x][y];
                            user[x][y]="  ";
                            x=x-1;
                            showmaps(user);
                         break;
                        } else{
                            user[x-1][y]=user[x][y];
                            user[x][y]="  ";
                            x=x-1;
                            showmaps(user);
                            break;

                        }
                    }

                case "d": //定义向右移动
                    if (user[x][y+1]=="H "){
                        showmaps(user);
                       break;
                    }else{
                        if ((user[x][y+1]=="o ") && (user[x][y+2]=="  ")){
                            user[x][y+2]=user[x][y+1];
                            user[x][y+1]=user[x][y];
                            user[x][y]="  ";
                            y=y+1;
                            showmaps(user);
                            break;
                        } else{
                            user[x][y+1]=user[x][y];
                            user[x][y]="  ";
                            y=y+1;
                            showmaps(user);
                            break;
                        }
                    }
                case "q":
                    break;
                default:
                    System.out.println("请输入你的命令：");
            }



        }
    }
}
